#import "BlockController.h"
#import "BLAuthentication.h"

@implementation BlockController

@synthesize name;


-(id)init{
    self = [ super init];
    if(self){
        name = @"YODA";
    }
    return self;
}
-(void)setname:(NSString *)tName{
    name = tName;
}

-(void) sendLoad{
    
    NSString *p=[[NSBundle mainBundle]resourcePath];
    NSString * str = [p stringByAppendingString:@"/ProxyFireWall"];
    NSArray * args3;
    NSString *p2=[@"" stringByAppendingString:str ];
    args3 = [NSArray arrayWithObjects:name,@"s", nil];
    NSLog(@"%@\n",p2);
    [[BLAuthentication shared] executeCommand:p2 withArgs:args3];
    
    
}
-(void) sendPath
{
    
    int i;
   
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    
    // Set array of file types
    
    NSArray *fileTypesArray;
    
    fileTypesArray = [NSArray arrayWithObjects:@"app",@"", nil];
    
    [openDlg setCanChooseFiles:YES];
    [openDlg setTitle:@"Add to blocklist"];
  
    [openDlg setAllowedFileTypes:fileTypesArray];
    [openDlg setPrompt:@"Add"];
    [openDlg setAllowsMultipleSelection:FALSE];
    
    if ( [openDlg runModal] == NSOKButton ) {
        
        NSArray *files = [openDlg URLs];
        
        for( i = 0; i < [files count]; i++ ) {
            [self setName:[[files objectAtIndex:i] path]];
            //BLAuthentication * BLAuth = [BLAuthentication alloc];//создание инструмента авторизации
            NSString *p=[[NSBundle mainBundle]resourcePath];
            
            NSString * str = [p stringByAppendingString:@"/ProxyFireWall"];
            NSArray * args3;
            NSString *p2=[@"" stringByAppendingString:str ];
            args3 = [NSArray arrayWithObjects:[[files objectAtIndex:i] path],@"s", nil];
            [[BLAuthentication shared] executeCommand:p2 withArgs:args3];
            
        }
        
    }
    
}


//-(IBAction)delPath :(id)sender

-(void) delPath:(NSString*)path{
    

            NSString *p=[[NSBundle mainBundle]resourcePath];
            
            NSString * str = [p stringByAppendingString:@"/ProxyFireWall"];
            NSArray * args3;
            NSString *p2=[@"" stringByAppendingString:str ];
            args3 = [NSArray arrayWithObjects:path,@"d", nil];
            [[BLAuthentication shared] executeCommand:p2 withArgs:args3];
            

}

//-(IBAction)load :(id)sender{
//    
//   // BLAuthentication * BLAuth = [BLAuthentication alloc];//создание инструмента авторизации
//    NSString *p=[[NSBundle mainBundle]resourcePath];
//    
//    NSString * pathToKext = [p stringByAppendingString:@"/FireWall.kext"];//путь к кексту
//    //смена прав доступа к кексту
//    NSArray * args;
//    args = [NSArray arrayWithObjects: @"-R", @"root:wheel", pathToKext, nil];
//    [BLAuth executeCommand:@"/usr/sbin/chown" withArgs: args];
//
//    //загрузка драйвера
//    NSArray * args2;
//    args2 = [NSArray arrayWithObjects: pathToKext, nil];
//    [BLAuth executeCommand:@"/sbin/kextload" withArgs: args2];
//
//}
-(IBAction)unload :(id)sender{
  //  BLAuthentication * BLAuth = [BLAuthentication alloc];//создание инструмента авторизации
    NSString *p=[[NSBundle mainBundle]resourcePath];
    
    NSString * pathToKext = [p stringByAppendingString:@"/FireWall.kext"];//путь к кексту
    
    NSArray * args4;
    args4 = [NSArray arrayWithObjects: pathToKext, nil];
    [[BLAuthentication shared] executeCommand:@"/sbin/kextunload" withArgs: args4];
}
@end
